
const app = getApp();
const api = app.apiInstance
import { getUserInfo } from '../../utils/store';
Page({
  data: {
    currentStep: 0,
    tabStatus: [{
      label: '未填写',
      value: ''
    }, {
      label: '进行中',
      value: 'process'
    }, {
      label: '已完成',
      value: 'finish'

    }],
    windowWidth: 0,
    id: '',
    formData: {},
    isEdit: true,

    sapId: '',
    userInfo: {
      sapid: '',
      userName: getUserInfo().siocPYName,
      nickName: getUserInfo().siocCnName,
      createTime: app.moment().format('YYYY-MM-DD HH:mm:ss'),
    },
    type: '',
    zongType: '',

  },
  async loadData(id) {
    if (id) {
      const formData = await api.get(`/v1/openApi/resource/change/details/${id}`);


      const current = formData.data.value.find(item => item.auditor == this.data.sapId&&item.remark.indexOf('验收')>0);
      console.log('current', current)
      const shenHeType = this.checkType(current.remark);
      console.log('shenHeType', shenHeType)

      console.log('current', current);
      this.setData({
        formData: formData.data,
        current: current,
        type: shenHeType,
        remark: current.remark
      })

      if(!this.data.isEdit){
        
        this.setData({
          readOnlyItem: formData.data.key.contentMap[shenHeType],
        })

      }


    }
  },

  onLoad(params) {
    
    console.log('params', params);
    this.setData({
      id: params.id,
      sapId:params.sapid||getUserInfo().siocSapNo,
      'userInfo.sapid':params.sapid||getUserInfo().siocSapNo,
      isEdit:params.isEdit=='false'?false:true
    })
    this.loadData(params.id);
    var systemInfo = wx.getSystemInfoSync();
    this.setData({
      windowWidth: systemInfo.windowWidth
    })

  },

  checkType(type) {

    if (type.indexOf('项目跟进人验收') > -1) {
      return 'shiYongBuMenYs'

    }
    if (type.indexOf('使用部门经理验收') > -1) {
      return 'shiYongBuMenJlYs'

    }
    

    if (type.indexOf('相关部门') > -1) {
      return 'xiangGuanBuMenYs'

    }
    if (type.indexOf('消防') > -1) {
      return 'xiaoFangBuMenYs'

    }
    if (type.indexOf('职业健康') > -1) {
      return 'zhiYeJianKangYs'

    }
    if (type.indexOf('环保') > -1) {
      return 'huanBaoYs'

    }
    if (type.indexOf('安全') > -1) {
      return 'anQuanYs'

    }
    if (type.indexOf('应急管理') > -1) {
      return 'yingJiGuanLiYs'

    }
    if (type.indexOf('应急管理审核') > -1) {
      return 'yingJiGlSh'

    }
    if (type.indexOf('分管副总') > -1) {
      return 'fenGuanFuZongYs'

    }
    if (type.indexOf('副总') > -1) {
      return 'fuZongYs'

    }
    if (type.indexOf('总经理') > -1) {
      return 'zongJingLiYs'
    }

  },

  async submit() {
    if(!this.data.isEdit){
      wx.navigateBack({
        delta: 1
      });
      return;
    }
    const current = this.data.current;
    console.log('current', current)



    return {
      shiYongBuMenYs: async () => {
        const shiYongBuMenYs = this.selectComponent("#shiYongBuMenYs").data;
        await api.post(`/v1/openApi/resource/change/completelyStep/${this.data.id}/${current.step}/${this.data.sapId}`);

        const content = Object.assign({}, { ...this.data.formData.key.contentMap }, { shiYongBuMenYs: { ...shiYongBuMenYs.form, userInfo: this.data.userInfo, title: '项目跟进人验收' } })
        console.log('content', content)
        const updateStatusParams = {
          id: this.data.id,
          sapid: this.data.sapId,
          data: Object.assign({}, { ...this.data.formData.key }, { content: JSON.stringify(content) }, { status: 'ToBeAcceptance' })
        }
        console.log('111', { ...updateStatusParams.data })
        //改变整个主数据
        await api.post(`/v1/openApi/resource/change/updateStatus/${this.data.id}/${this.data.sapId}`, { ...updateStatusParams.data });
        wx.showToast({
          title: '确认成功',
          icon: 'none',
          duration: 2000
        })
        wx.navigateTo({
          url: `/subPages/bianGengSq/bianGengSq`,
        })
        return;
      },

      qiTaBuMenYs: async () => {
        const qiTaBuMenYs = this.selectComponent("#qiTaBuMenYs").data;
        await api.post(`/v1/openApi/resource/change/completelyStep/${this.data.id}/${current.step}/${this.data.sapId}`);


        let remark = ''
        if(this.data.type=='xiangGuanBuMenYs'){
          remark =this.data.remark.split("——")[1]
          this.data.formData.key.contentMap.other = this.data.formData.key.contentMap.other||[]
          this.data.formData.key.contentMap.other.push({[remark]:{ ...qiTaBuMenYs.form, userInfo: this.data.userInfo, title: remark+'验收' }}) 
        }
        else{
          remark = this.data.remark
        }
        const content = Object.assign({}, { ...this.data.formData.key.contentMap }, 
          (this.data.type=='xiangGuanBuMenYs'?{ other:this.data.formData.key.contentMap.other}:{ [this.data.type]: { ...qiTaBuMenYs.form, userInfo: this.data.userInfo, title: remark+'验收' }})
        
        )
        const updateStatusParams = {
          id: this.data.id,
          sapid: this.data.sapId,
          data: Object.assign({}, { ...this.data.formData.key }, { content: JSON.stringify(content) }, { status: this.data.type=='zongJingLiYs'?'AcceptanceCompleted':'ToBeAcceptance' })
        }
        //改变整个主数据
        await api.post(`/v1/openApi/resource/change/updateStatus/${this.data.id}/${this.data.sapId}`, { ...updateStatusParams.data });
        wx.showToast({
          title: '确认成功',
          icon: 'none',
          duration: 2000
        })
        wx.navigateTo({
          url: `/subPages/bianGengSq/bianGengSq`,
        })
        return;
      }

    }[this.data.type=='shiYongBuMenYs'?'shiYongBuMenYs':'qiTaBuMenYs']()
      

  }

})
